package com.lkx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkx.common.CustomException;
import com.lkx.dto.DishDto;
import com.lkx.dto.SetmealDto;
import com.lkx.mapper.CategoryMapper;
import com.lkx.mapper.SetMealMapper;
import com.lkx.pojo.*;
import com.lkx.service.SetMealService;
import com.lkx.service.SetmealDishService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName: com.lkx.service.impl.SetMealServiceImpl
 * @author: likaixin
 * @create: 2022年11月22日 19:37
 * @desc:
 */
@Service
@Slf4j
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetMealService setMealService;

    @Value("${reggie.path}")
    private String basePath;

    /**
     * @Date:  2022-11-24
     * @Time:  16:59
     * @Param: [setmealDto]
     * @return: void
     * @desc: 新增套餐，同时需要保存套餐和菜品的关联关系
     **/
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
       setmealDishes.stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
    /**
     * @Date:  2022-11-24
     * @Time:  19:39
     * @Param: [ids]
     * @return: void
     * @desc: 删除套餐，同时需要删除套餐和菜品的关联数据
     **/
    @Override
    @Transactional
    public void removeWithDish(Long[] ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        int count = super.count(lqw);
         if (count>0){
             throw new CustomException("套餐正在售卖中，不能删除");
         }


        List<Setmeal> list = setMealService.listByIds(Arrays.stream(ids).collect(Collectors.toList()));
        for (int i = 0; i < list.size(); i++) {
            File file = new File(basePath, list.get(i).getImage());
            System.out.println(file);
            file.delete();
        }

        super.removeByIds(Arrays.stream(ids).collect(Collectors.toList()));
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper);
    }

    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        //先根据id查询dish菜品
        Setmeal setmeal = this.getById(id);
        //创建扩展dish的对象
        SetmealDto setmeaDto=new SetmealDto();
        //然后copy，把dish对象的值 copy到dishDto的flavors字段里
        BeanUtils.copyProperties(setmeal,setmeaDto);
        //创建条件查询对象
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();

        qw.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(qw);

        setmeaDto.setSetmealDishes(list);

        return setmeaDto;
    }

    @Override
    public void updateSetMeal(SetmealDto dto) {

        this.updateById(dto);


    }
}
